package com.pay.trans.alipay;

import com.pay.utils.ConfigUtils;
import com.pay.utils.SSLClient;
import com.pay.utils.SignUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-6-30.
 *
 */
public class AlipayScanCodeTest0119 {
    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpClient = new SSLClient();
        HttpPost httpPost = new HttpPost(ConfigUtils.getProperty("trans_url"));

        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("requestNo",new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));
        nvps.add(new BasicNameValuePair("version","V1.0"));
        nvps.add(new BasicNameValuePair("productId","0119"));
        nvps.add(new BasicNameValuePair("transId","10"));
        nvps.add(new BasicNameValuePair("merNo",ConfigUtils.getProperty("merchant_no")));

        nvps.add(new BasicNameValuePair("orderDate",new SimpleDateFormat("yyyyMMdd").format(new Date())));
        nvps.add(new BasicNameValuePair("orderNo",new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));
        nvps.add(new BasicNameValuePair("clientIp","127.0.0.1"));

        nvps.add(new BasicNameValuePair("returnUrl", "http://localhost:80"));
        nvps.add(new BasicNameValuePair("notifyUrl", "http://localhost:80"));
        nvps.add(new BasicNameValuePair("transAmt", "100"));
        nvps.add(new BasicNameValuePair("commodityName", "test"));

        nvps.add(new BasicNameValuePair("signature", SignUtils.signData(nvps)));

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
            //System.out.println("============>"+nvps.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            String str = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
            System.out.println("============>"+str);

        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}
